package com.rxkj.controller;

import com.rxkj.authen.PassToken;
import com.rxkj.authen.RoleLevel;
import com.rxkj.entity.sys.RoleEntity;
import com.rxkj.entity.sys.UserInfo;
import com.rxkj.entity.sys.UserVo;
import com.rxkj.service.UserService;
import com.rxkj.syslog.SysOPLog;
import com.rxkj.utils.JWTAuthUtil;
import com.rxkj.utils.MD5Util;
import com.rxkj.utils.PageData;
import com.rxkj.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/sys")
@Api(tags = "系统登陆")
public class LoginController {

    private static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @ApiOperation("系统用户登录")
    @PassToken
    @SysOPLog(logType = "sign", userName = "#ui.userName", content = "登陆")
    public Map<String, Object> sysLogin(@RequestBody UserInfo ui, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        UserInfo qui = userService.getUserByName(ui.getUserName());
        if (qui != null) {
            if (qui.getUserPwd().equals(MD5Util.MD5(ui.getUserPwd()))) {
                //账户名密码都正确
                //要包含角色等级
                String token = JWTAuthUtil.getToken(qui);
                res.put("code", 200);
                res.put("message", "登陆成功！");
                res.put("token", token);
                qui.setUserPwd(null);
                res.put("user", qui);
            } else {
                res.put("code", 201);
                res.put("message", "用户名或者密码错误！");
            }
        } else {
            res.put("code", 201);
            res.put("message", "用户名或者密码错误！");
        }
        return res;
    }

    @PostMapping("/editPassword")
    @ApiOperation("修改密码")
    @RoleLevel(2)
    @SysOPLog(logType = "update", userName = "#ui.userName", content = "修改密码")
    public ResponseResult editPassword(@RequestBody UserVo ui, HttpServletRequest request) {
        UserInfo user = userService.getUserByName(ui.getUserName());
        if (user != null) {
            if (!user.getUserPwd().equals(MD5Util.MD5(ui.getPassword()))) {
                return ResponseResult.error("原密码不正确");
            }
            int info = userService.editPassword(user.getId(), MD5Util.MD5(ui.getNewPassword()));
            return info > 0 ? ResponseResult.success() : ResponseResult.error();
        }
        return ResponseResult.error("数据未找到");
    }

    @PostMapping("/getUser")
    @ApiOperation(value = "模糊查询所有用户")
//    @RoleLevel(2)
    public ResponseResult getUser(int currentPage, int pageSize, @RequestBody UserInfo userInfo) {
        PageData<UserInfo> pg = userService.getUser(currentPage, pageSize, userInfo);
        return ResponseResult.success("查询成功", pg);
    }

    @PostMapping("/addUser")
    @ApiOperation(value = "添加用户")
//    @RoleLevel(2)
    public ResponseResult AddUser(@RequestBody UserInfo userInfo) {
        int pg = userService.AddUser(userInfo);
        return pg > 0 ? ResponseResult.success() : ResponseResult.error();
    }

    @GetMapping(value = "getByIdUser")
    @ApiOperation(value = "根据id查询用户信息")
//    @RoleLevel(2)
    public ResponseResult getByIdUser(Integer id) {
        if (id < 1) {
            return ResponseResult.error("请求参数无效");
        }
        UserInfo info = userService.getByIdUser(id);
        return info != null ? ResponseResult.success(info) : ResponseResult.error("数据未找到");
    }

    @GetMapping(value = "getByIdRole")
    @ApiOperation(value = "根据id查询角色信息")
//    @RoleLevel(2)
    public ResponseResult getByIdRole(Integer id) {
        if (id < 1) {
            return ResponseResult.error("请求参数无效");
        }
        RoleEntity info = userService.getByIdRole(id);
        return info != null ? ResponseResult.success(info) : ResponseResult.error("数据未找到");
    }

    @PostMapping(value = "updateUser")
    @ApiOperation(value = "修改用户信息")
    @RoleLevel(2)
    public ResponseResult updateUser(@RequestBody UserInfo userInfo) {
        int info = userService.updateUser(userInfo);
        return info > 0 ? ResponseResult.success() : ResponseResult.error("修改失败");
    }

    @PostMapping(value = "updateRole")
    @ApiOperation(value = "修改角色信息")
//    @RoleLevel(2)
    public ResponseResult updateRole(@RequestBody RoleEntity roleEntity) {
        int info = userService.updateRole(roleEntity);
        return info > 0 ? ResponseResult.success() : ResponseResult.error("修改失败");
    }

    @GetMapping(value = "deleteUser")
    @ApiOperation(value = "删除用户信息")
    @RoleLevel(2)
    public ResponseResult deleteUser(Integer id) {
        int info = userService.deleteUser(id);
        return info > 0 ? ResponseResult.success() : ResponseResult.error();
    }

    @PostMapping("/getRole")
    @ApiOperation(value = "模糊查询所有角色")
//    @RoleLevel(2)
    public ResponseResult getRole(int currentPage, int pageSize, @RequestBody RoleEntity roleEntity) {
        PageData<RoleEntity> pg = userService.getRole(currentPage, pageSize, roleEntity);
        return ResponseResult.success("查询成功", pg);
    }

    @PostMapping("/addRole")
    @ApiOperation(value = "添加角色")
//    @RoleLevel(2)
    public ResponseResult addRole(@RequestBody RoleEntity roleEntity) {
        try {
            userService.addRole(roleEntity);
            return ResponseResult.success();
        } catch (Exception e) {
            return ResponseResult.error();
        }
    }

    @GetMapping(value = "deleteRole")
    @ApiOperation(value = "删除角色信息")
//    @RoleLevel(2)
    public ResponseResult deleteRole(Integer id) {
        try {
            userService.deleteRole(id);
            return ResponseResult.success();
        } catch (Exception e) {
            return ResponseResult.error();
        }
    }

}
